import { G } from '../common'
export let firstConcat = {}

cc.Class({
    extends: cc.Component,
    properties: {
        prefabQiu: cc.Prefab,
        is_Deal: true,
        num: cc.Label,
        juicePrefab: cc.Prefab, //果汁
        // 得分音效资源
        scoreAudio: {
            default: null,
            type: cc.AudioClip
        },
    },

    init(data) {
        let is_anim = data.is_anim ? data.is_anim : false;
        this.DrawBall(data.num, this.node, is_anim);
    },

    DrawBall(num, ballnode, is_anim = false) {
        var num = parseInt(num);
        // console.log("球的大小：",num);
        // console.log("球的属性：",ballnode);
        // console.log(num, G.maxNum)
        if (num > G.maxNum) G.maxNum = num
            //小球赋值
        ballnode.getChildByName('num').getComponent(cc.Label).string = num;


        if (is_anim) {
            //播放动画
            // var anim = ballnode.getComponent(cc.Animation);
            // anim.play();
            // anim.on('finished',()=>{
            //     ballnode.setScale(G.BallArr[num]);
            // },this);

            cc.tween(this.node)
                // 0s 时，node 的 scale 还是 1
                .to(0.03, { scale: G.BallArr[num] * 1.4 })
                .to(0.04, { scale: G.BallArr[num] * 1.2 })
                // 1s 时，执行完第一个 action，scale 为 2
                .to(0.05, { scale: G.BallArr[num] })
                // 2s 时，执行完第二个 action，scale 为 3
                .start()
                // 调用 start 开始执行 cc.tween
                //ballnode.setScale(G.BallArr[num]);
        } else {
            ballnode.setScale(G.BallArr[num]);
        }


        //画图片
        var thenum = Math.log(num) / Math.log(2);
        cc.resources.load("img/planet_list/fruit", cc.SpriteAtlas, function(err, atlas) {
            ballnode.getComponent(cc.Sprite).spriteFrame = atlas.getSpriteFrame('fruit_' + thenum);
        });
    },

    onFinished: function() {

    },

    // use this for initialization
    onLoad: function() {
        // this.num.string = 2
        cc.director.getCollisionManager().enabled = true;

        // cc.resources.preload('img/planet_list/juice', cc.SpriteAtlas);
        cc.resources.preload('img/planet_list/fruit', cc.SpriteAtlas);
    },

    onCollisionEnter: function(other, self) {

        if (self.node && other.node) {
            const selfNum = self.node.getChildByName('num').getComponent(cc.Label);
            const otherNum = other.node.getChildByName('num').getComponent(cc.Label);
            let iswaterM = false;
            if (selfNum.string == 2048 || otherNum.string == 2048) iswaterM = true;

            if (selfNum.string === otherNum.string && selfNum.string != 0 && otherNum.string != 0) {
                self.node.emit('sameContact', self, other, selfNum.string * 2, iswaterM);
            } else {
                let mousemove = cc.find("Canvas/Mousemove");
                let line = cc.find("Canvas/line");
                //const r = self.node.height*self.node.scale/2;//半径
                let qiu_y;
                if (self.node.y < 0) {
                    qiu_y = mousemove.y - Math.abs(self.node.y);
                    //G.maxHeight = G.maxHeight > line.y ? this.GameOver(G.maxHeight) : qiu_y;
                }
            }
        }
    },

    GameOver(max) {
        // console.log("mx:",max)
        const result = cc.find("Canvas/Result");
        setTimeout(function() {
            result.active = true;
        }, 600)

    }

});